<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\User;

class UserResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'key' => $this->key,
            'username' => $this->username,
            'email' => $this->email,
            'score_count' => $this->score_count,
            'topic_count' => $this->topic_count,
            'comment_count' => $this->comment_count,
            'following_count' => $this->following_count,
            'fans_count' => $this->fans_count,
            'avatar' => $this->getAvatar($this->resource),
            'role_names' => ''
        ];
    }

    private function getAvatar($user)
    {
        $routeName = request()->route()->getName();
        if ($routeName == 'api.user.show') {
            return attachmentUrl($user->avatar, 120);
        }
        return attachmentUrl($user->avatar, 80);
    }
}
